package com.noreply.springannotation;

import com.noreply.springannotation.bean.Person;
import com.noreply.springannotation.bean.User;
import com.noreply.springannotation.config.UserConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAnnotationApplication {

	public static void main(String[] args) {
        ApplicationContext applicationContext1 =
                new ClassPathXmlApplicationContext("spring-bean.xml");
//        User user =  applicationContext.getBean(User.class);
//        System.out.println(user.toString());
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(UserConfig.class);

        // 因为注册了多个User类型的bean,如果通过类型User.class取bean,会报错
        // applicationContext.getBean(User.class);
        User user = (User)applicationContext.getBean("user");
        User user1 = (User)applicationContext.getBean("user1");
        User user2 = (User)applicationContext.getBean("user2");
        System.out.println(user.toString());
        System.out.println(user1.toString());
        System.out.println(user2.toString());

        Person person = applicationContext.getBean(Person.class);
        System.out.println(person);

//        String[] beans = applicationContext.getBeanDefinitionNames();
//        for (int i = 0; i < beans.length; i++) {
//            System.out.println(beans[i]);
//        }

	}

}
